#include "IO.hh"
#include <iostream>
#include <limits>
#include <cctype>
#include <algorithm>

namespace IO {
    int getNumber() {
        int number = 0;
        bool valid = false;
        char c;
        while (!valid) {
            valid = true;
            number = 0;
            while (std::cin.get(c)) {
                if (c == '\n') {
                    break;
                }
                if (!std::isdigit(c)) {
                    std::cout << "Invalid input. Try again: ";
                    valid = false;
                    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                    break;
                } else {
                    number = number * 10 + (c - '0');
                }
            }
        }
        return number;}
    double getAmount() {
    double value = 0.0;
    std::string input;
    bool valid = false;

    while (!valid) {
        valid = true;
        input.clear();
        // Read one line of input at once
        std::getline(std::cin, input);
        // Remove whitespace from input
        input.erase(remove_if(input.begin(), input.end(), ::isspace), input.end());
        // Validate characters and format
        int dotCount = 0;
        for (size_t i = 0; i < input.size(); ++i) {
            char ch = input[i];
            if (ch == '.') {
                dotCount++;
                if (dotCount > 1) {
                    valid = false;
                    break;
                }
            } else if (!std::isdigit(ch)) {
                if (ch == '-' && i == 0) continue;  // allow negative at beginning
                valid = false;
                break;}
        }
        if (!valid || input.empty()) {
            std::cout << "Invalid amount. ";
            continue;
        }
        // Manual conversion
        value = 0.0;
        bool isNegative = false;
        bool afterDot = false;
        double fractionFactor = 0.1;

        size_t i = 0;
        if (input[i] == '-') {
            isNegative = true;
            i++;}

        if (i >= input.size()) {
            std::cout << "Invalid number format. ";
            continue;}

        for (; i < input.size(); ++i) {
            char ch = input[i];
            if (ch == '.') {
                if (afterDot) {
                    std::cout << "Invalid number format. ";
                    valid = false;
                    break;
                }
                afterDot = true;
            } else if (std::isdigit(ch)) {
                int digit = ch - '0';
                if (!afterDot) {
                    value = value * 10 + digit;
                } else {
                    value += digit * fractionFactor;
                    fractionFactor *= 0.1;
                }
            } else {
                std::cout << "Invalid number format. ";
                valid = false;
                break;
            }
        }
        if (!valid) continue;
        if (isNegative) value = -value;
    }

    return value;
}
    std::string getString() {
        std::string input;
        std::getline(std::cin, input);
        return input;
    }
    bool getYesOrNo() {
        std::string input;
        while (true) {
            std::getline(std::cin, input);
            if (input == "y" || input == "Y") return true;
            if (input == "n" || input == "N") return false;
            std::cout << "Please enter Y or N: ";
        }
    }
}